package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntCdbgmdtldBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntCdbgmdtldBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntCdbgmdBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntCdbgmdBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntCdbgmdBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntCdbgmdBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntCdbgmdBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土稠度、观密度试验检测记录Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntCdbgmdBsInfoServiceImpl implements IReportSnhntCdbgmdBsInfoService {

    private final ReportSnhntCdbgmdBsInfoMapper baseMapper;
    private final ReportSnhntCdbgmdtldBaDetailMapper detailMapper;

    /**
     * 查询水泥混凝土稠度、观密度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntCdbgmdBsInfoVo queryById(Long excelId) {
        ReportSnhntCdbgmdBsInfoVo vo = baseMapper.selectVoById(excelId);
        LambdaQueryWrapper<ReportSnhntCdbgmdtldBaDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReportSnhntCdbgmdtldBaDetail::getExcelId, excelId);
        List<ReportSnhntCdbgmdtldBaDetail> detailList = detailMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询水泥混凝土稠度、观密度试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSnhntCdbgmdBsInfoVo> queryPageList(ReportSnhntCdbgmdBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntCdbgmdBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntCdbgmdBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土稠度、观密度试验检测记录列表
     */
    @Override
    public List<ReportSnhntCdbgmdBsInfoVo> queryList(ReportSnhntCdbgmdBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntCdbgmdBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntCdbgmdBsInfo> buildQueryWrapper(ReportSnhntCdbgmdBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntCdbgmdBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntCdbgmdBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntCdbgmdBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntCdbgmdBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntCdbgmdBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntCdbgmdBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntCdbgmdBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntCdbgmdBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntCdbgmdBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntCdbgmdBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportSnhntCdbgmdBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportSnhntCdbgmdBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getLaiysj() != null, ReportSnhntCdbgmdBsInfo::getLaiysj, bo.getLaiysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSnhntCdbgmdBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytj()), ReportSnhntCdbgmdBsInfo::getShiytj, bo.getShiytj());
        lqw.eq(StringUtils.isNotBlank(bo.getWend()), ReportSnhntCdbgmdBsInfo::getWend, bo.getWend());
        lqw.eq(StringUtils.isNotBlank(bo.getXiangdsd()), ReportSnhntCdbgmdBsInfo::getXiangdsd, bo.getXiangdsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntCdbgmdBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntCdbgmdBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntCdbgmdBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntCdbgmdBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntzl()), ReportSnhntCdbgmdBsInfo::getHunntzl, bo.getHunntzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiaobfs()), ReportSnhntCdbgmdBsInfo::getJiaobfs, bo.getJiaobfs());
        lqw.eq(StringUtils.isNotBlank(bo.getChoudsjbh1()), ReportSnhntCdbgmdBsInfo::getChoudsjbh1, bo.getChoudsjbh1());
        lqw.eq(StringUtils.isNotBlank(bo.getChoudsjbh2()), ReportSnhntCdbgmdBsInfo::getChoudsjbh2, bo.getChoudsjbh2());
        lqw.eq(bo.getWeibcdczbh11() != null, ReportSnhntCdbgmdBsInfo::getWeibcdczbh11, bo.getWeibcdczbh11());
        lqw.eq(bo.getWeibcdczbh12() != null, ReportSnhntCdbgmdBsInfo::getWeibcdczbh12, bo.getWeibcdczbh12());
        lqw.eq(bo.getWeibcdczbh21() != null, ReportSnhntCdbgmdBsInfo::getWeibcdczbh21, bo.getWeibcdczbh21());
        lqw.eq(bo.getWeibcdczbh22() != null, ReportSnhntCdbgmdBsInfo::getWeibcdczbh22, bo.getWeibcdczbh22());
        lqw.eq(bo.getWeibcdczbh1pjz() != null, ReportSnhntCdbgmdBsInfo::getWeibcdczbh1pjz, bo.getWeibcdczbh1pjz());
        lqw.eq(bo.getWeibcdczbh2pjz() != null, ReportSnhntCdbgmdBsInfo::getWeibcdczbh2pjz, bo.getWeibcdczbh2pjz());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntCdbgmdBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntCdbgmdBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntCdbgmdBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntCdbgmdBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntCdbgmdBsInfo::getRiq, bo.getRiq());
        return lqw;
    }

    /**
     * 新增水泥混凝土稠度、观密度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportSnhntCdbgmdBsInfoBo bo) {
        ReportSnhntCdbgmdBsInfo add = BeanUtil.toBean(bo, ReportSnhntCdbgmdBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            //插入子表数据；4行
            List<ReportSnhntCdbgmdtldBaDetail> detailList = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                ReportSnhntCdbgmdtldBaDetail detail = new ReportSnhntCdbgmdtldBaDetail();
                detail.setExcelId(bo.getExcelId());
                detailList.add(detail);
            }
            detailMapper.insertBatch(detailList);
        }
        return flag;
    }

    /**
     * 修改水泥混凝土稠度、观密度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSnhntCdbgmdBsInfoBo bo) {
        ReportSnhntCdbgmdBsInfo update = BeanUtil.toBean(bo, ReportSnhntCdbgmdBsInfo.class);
        validEntityBeforeSave(update);
        //修改子表数据；
        List<ReportSnhntCdbgmdtldBaDetail> detailList = bo.getDetailList();
        detailMapper.updateBatchById(detailList);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntCdbgmdBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土稠度、观密度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
